<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('main.php');

class User extends Main {

	public function __construct()
	{
		parent::__construct();
	}

	// download process
	public function download($filename)
	{
		// load the download helper
		$this->load->helper(array('download', 'file'));

		$data = read_file('./assets/files/'.$filename);

		if(!force_download($filename, $data))
		{
			show_error("Something went wrong while downloading the file, please try again <a href='". $this->input->server('HTTP_REFERER') ."'>here</a>");
		}
	}

	// process upload
	public function upload()
	{	
		// load the file helper
		$this->load->helper('file');

		// file upload configuration
		$config = array(
			'upload_path' => './assets/files/',
			'max_size' => '10000', 
			'allowed_types' => '*'
		);

		$this->load->library('upload', $config);

		if (!$this->upload->do_upload())
		{
			$this->session->set_flashdata('profile_errors', $this->upload->display_errors());
		}
		else
		{
			$file_info = $this->upload->data();

			$upload_info = array(
				'file_name' => $file_info['file_name'],
				'file_path' => ltrim($config['upload_path'], '.') . $file_info['file_name'],
				'user_id' => $this->session->userdata('user_id')
			);

			// load the file model
			$this->load->model('file_model');

			// insert into the files table
			$this->file_model->create($upload_info);

			$this->session->set_flashdata('profile_success', '<p>You have successfully uploaded your file!</p>');
		}

		redirect(base_url('/user/profile'));
	}

	// PROFILE PAGE / FILE MANAGER
	public function profile()
	{
		// load the file and download helper
		$this->load->helper(array('file', 'download', 'form'));

		// load the file model
		$this->load->model('file_model');

		// get all user files
		$this->view_data['files'] = $this->file_model->get_by_user_id($this->session->userdata('user_id'));

		// load the news model
		$this->load->model('news_model');

		// get all news updates
		$this->view_data['news'] = $this->news_model->get();

		$this->view_data['styles'][] = 'profile';
		$this->render_view('user/profile');
		$this->parser->parse('templates/admin', $this->view_data);
	}

	// LOGIN
	public function login()
	{
		$this->render_view('user/login');
		$this->parser->parse('templates/base', $this->view_data);
	}

	public function process_login()
	{
		$this->load->model('user_model');

		$check_user = $this->user_model->get_by_email($this->input->post('email'));
		$error_count = 0;
		$error_message = "<p>Invalid email/password combination</p>";

		if($check_user)
		{
			$this->load->library('encrypt');

			$decrypted_password = $this->encrypt->decode($check_user['password']);

			if($decrypted_password != $this->input->post('password'))
			{
				$error_count++;
			}
			else if($this->input->post('user_type') != $check_user['user_level'])
			{
				$error_message = "<p>You are not a ". (($check_user['user_level'] != 1) ? "student" : "professor") ."</p>";
				$error_count++;
			}
		}
		else
		{
			$error_count++;
		}

		if($error_count > 0)
		{
			$this->session->set_flashdata('login_errors', $error_message);
			redirect('/user/login');
		}
		else
		{
			$this->session->set_userdata('user_id', $check_user['id']);
			redirect('/user/profile');
		}
	}

	// REGISTER
	public function register()
	{
		$this->render_view('user/register');

		$this->parser->parse('templates/base', $this->view_data);
	}

	public function process_register()
	{
		$this->load->library('form_validation');

		// set validation rules
		$this->form_validation->set_rules('first_name', 'First name', 'required|max_length[30]');
		$this->form_validation->set_rules('last_name', 'Last name', 'required|max_length[30]');
		$this->form_validation->set_rules('email', 'Email address', 'required|valid_email|is_unique[users.email]');
		$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
		$this->form_validation->set_rules('password_confirmation', 'Password confirmation', 'matches[password]');

		if($this->form_validation->run() === FALSE)
		{
			$errors = validation_errors();

			if($this->input->post('user_type') == 0)
			{
				$errors .= "<p>Please select a User type</p>";
			}

			$this->session->set_flashdata('register_errors', $errors);
		}
		else
		{
			$this->load->model('user_model');
			$this->load->library('encrypt');

			$user_info = array(
				'first_name' => $this->input->post('first_name'),
				'last_name' => $this->input->post('last_name'),
				'email' => $this->input->post('email'),
				'password' => $this->encrypt->encode($this->input->post('password')),
				'user_level' => $this->input->post('user_type')
			);

			$create_user = $this->user_model->create($user_info);

			if($create_user)
			{
				$this->session->set_flashdata('register_success', '<p>You have successfully registered!</p>');
			}
			else
			{
				$this->session->set_flashdata('register_errors', '<p>Something went wrong while your registration. Please try again</p>');
			}

		}

		redirect('/user/register');
	}
}